<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<api-documentation-page-title [api]="api"></api-documentation-page-title>
<mat-card>
  <div class="header">
    <h3 class="header__title">
      {{ pageTitle }}
      <img *ngIf="iconUrl" [ngSrc]="iconUrl" height="24" width="24" alt="{{ iconTooltip }} logo" [matTooltip]="iconTooltip" />
    </h3>

    <div class="header__location">
      In
      <api-documentation-breadcrumb [breadcrumbs]="breadcrumbs" [canNavigate]="false"></api-documentation-breadcrumb>
    </div>
    <div class="header__actions">
      <ng-container *gioPermission="{ anyOf: ['api-documentation-d'] }">
        <button
          class="header__delete"
          mat-stroked-button
          *ngIf="mode === 'edit'"
          (click)="deletePage()"
          [disabled]="!!page?.generalConditions"
          [matTooltip]="!!page?.generalConditions ? 'Cannot delete if used as General Conditions' : undefined"
        >
          Delete page
        </button>
      </ng-container>

      <button class="header__exit" mat-stroked-button (click)="goBackToPageList()">
        {{ exitLabel }}
      </button>
    </div>
  </div>
  <div class="stepper">
    <form [formGroup]="form">
      <mat-stepper linear color="accent" [disableRipple]="true">
        <mat-step>
          <ng-template matStepLabel>Configure page</ng-template>
          <div class="stepper__content">
            <form [formGroup]="stepOneForm">
              <mat-form-field>
                <mat-label>Name</mat-label>
                <input matInput formControlName="name" placeholder="Name" autofocus />
                <mat-error *ngIf="stepOneForm.controls.name?.errors?.required">Name is required</mat-error>
                <mat-error *ngIf="stepOneForm.controls.name?.errors?.unique">Name already exists in this folder</mat-error>
              </mat-form-field>
              <api-documentation-visibility formControlName="visibility"></api-documentation-visibility>
            </form>
          </div>
          <div class="stepper__actions">
            <button mat-flat-button color="primary" matStepperNext [disabled]="stepOneForm.invalid">Next</button>
          </div>
        </mat-step>
        <mat-step *ngIf="mode === 'create'">
          <ng-template matStepLabel>Determine source</ng-template>
          <div class="stepper__content">
            <div class="mat-subtitle-2">Page source</div>

            <gio-form-selection-inline class="gio-radio-group stepper__content__source" formControlName="source">
              <gio-form-selection-inline-card value="FILL">
                <gio-form-selection-inline-card-content icon="gio:edit-pencil">
                  <gio-card-content-title>Fill in the content myself</gio-card-content-title>
                </gio-form-selection-inline-card-content>
              </gio-form-selection-inline-card>
              <gio-form-selection-inline-card value="IMPORT">
                <gio-form-selection-inline-card-content icon="gio:down-circle">
                  <gio-card-content-title>Import from file</gio-card-content-title>
                </gio-form-selection-inline-card-content>
              </gio-form-selection-inline-card>
              <gio-form-selection-inline-card value="EXTERNAL" [disabled]="true">
                <gio-form-selection-inline-card-content icon="gio:language">
                  <gio-card-content-title>Import from source (URL)</gio-card-content-title>
                  <gio-card-content-subtitle><div class="gio-badge-primary">Coming soon</div></gio-card-content-subtitle>
                </gio-form-selection-inline-card-content>
              </gio-form-selection-inline-card>
            </gio-form-selection-inline>
          </div>
          <div class="stepper__actions">
            <button mat-flat-button color="primary" matStepperNext>Next</button>
            <button mat-stroked-button matStepperPrevious>Previous</button>
          </div>
        </mat-step>
        <mat-step>
          <ng-template matStepLabel>{{ step3Title }}</ng-template>
          <div class="stepper__content">
            <api-documentation-content
              formControlName="content"
              [published]="page?.published"
              [pageType]="pageType"
              *ngIf="form.value?.source === 'FILL'"
            ></api-documentation-content>
            <api-documentation-file-upload
              formControlName="content"
              [pageType]="pageType"
              *ngIf="form.value?.source === 'IMPORT'"
            ></api-documentation-file-upload>
            <mat-error *ngIf="stepOneForm.controls.content?.errors?.required">Page content cannot be empty</mat-error>
          </div>
          <div class="stepper__actions">
            <ng-container *ngIf="mode === 'create'">
              <ng-container *gioPermission="{ anyOf: ['api-documentation-c'] }">
                <button
                  *gioPermission="{ anyOf: ['api-documentation-u'] }"
                  mat-flat-button
                  color="primary"
                  [disabled]="form.invalid"
                  (click)="createAndPublish()"
                >
                  Save and publish
                </button>
                <button mat-stroked-button [disabled]="form.invalid" (click)="create()">Save</button>
              </ng-container>
            </ng-container>

            <ng-container *ngIf="mode === 'edit' && page">
              <ng-container *ngIf="!page.published">
                <button
                  *gioPermission="{ anyOf: ['api-documentation-u'] }"
                  mat-flat-button
                  color="primary"
                  [disabled]="form.invalid || form.pristine || formUnchanged"
                  (click)="updateAndPublish()"
                >
                  Save and publish
                </button>
                <button
                  *gioPermission="{ anyOf: ['api-documentation-u'] }"
                  mat-stroked-button
                  [disabled]="form.invalid || form.pristine || formUnchanged"
                  (click)="update()"
                >
                  Save
                </button>
              </ng-container>

              <ng-container *ngIf="page.published">
                <button
                  *gioPermission="{ anyOf: ['api-documentation-u'] }"
                  mat-flat-button
                  color="primary"
                  [disabled]="form.invalid || form.pristine || formUnchanged"
                  (click)="update()"
                >
                  Publish changes
                </button>
              </ng-container>
            </ng-container>

            <button mat-stroked-button matStepperPrevious>Previous</button>
          </div>
        </mat-step>
      </mat-stepper>
    </form>
  </div>
</mat-card>
